package cn.itsource.pethome_0310.org.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.StrUtil;
import cn.itsource.pethome_0310.basic.domain.BaseQuery;
import cn.itsource.pethome_0310.basic.domain.JSONResult;
import cn.itsource.pethome_0310.org.domain.Department;

import cn.itsource.pethome_0310.org.mapper.DeptMapper;
import cn.itsource.pethome_0310.org.service.DeptService;
import cn.itsource.pethome_0310.util.TreeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Array;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptMapper deptMapper;

    @Override
    public List<Department> findAll() {
        return deptMapper.findAll();
    }

    @Override
    public JSONResult findPage(BaseQuery baseQuery) {
        //1.调用分页对象
        PageHelper.startPage(baseQuery.getPage(),baseQuery.getRows());
        //2.查询所有
        List<Department> all = deptMapper.findPage(baseQuery);
        //3.封装分页对象
        PageInfo<Department> pageInfo = new PageInfo<>(all);

//        /****************************************************
//        *
//        *使用tkmybatis完成高级查询
//        *
//        ****************************************************/
//        all.stream().forEach(department -> {
//            //1,查询上级部门的名称
//            Department parent = deptMapper.findByparentId(department.getParentId());
//            department.setParent(parent.getName());
//            //2.查询部门经理
//
//        });

        return JSONResult.success(pageInfo);
    }

    /**
     * 查询部门的树形结构
     * @return
     */
    @Override
    public JSONResult findTree() {
        //1,查所有
        List<Department> departments =  deptMapper.findAll();
        //2.调用工具类
        List<Department> tree = TreeUtil.getTree(departments);

        return JSONResult.success(tree);
    }

    /**
     *
     * @param department
     * @return
     */
    @Override
    public JSONResult add(Department department) {
        //insertSelective 是可以给默认值,只会新增你有的字段
        //insert 会新增你对象有的字段,其余字段全部设置为null
        deptMapper.insertSelective(department);
        log.info("当前新增的部门id为:{}" ,department.getId());
        return JSONResult.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public JSONResult delete(Long id) {
        Department department = new Department(id,Boolean.FALSE);
        deptMapper.updateByPrimaryKeySelective(department);
        return JSONResult.success();
    }

    @Override
    public JSONResult batchDelete(List<Long> ids) {
        deptMapper.batchDelete(ids);
        return null;
    }

    public static void main(String[] args) {
        List<Department> departments = Arrays.asList(new Department(1L, "zhangs"), new Department(2L, "lis"));
        //将一个集合转换成一个新的集合,可以改变这个集合的泛型
        List<Long> ids = departments.stream().map(department -> department.getId()).collect(Collectors.toList());
        System.out.println(ids);
        String sql = " SELECT dept1.*,dept2.`name` AS parent,emp.username AS manager FROM " +
                "t_department dept1" +
                "left JOIN t_department dept2 ON dept1.parent_id = dept2.id " +
                "left JOIN t_employee emp ON dept1manager_id = emp.id" +
                "        where 1=1";
    }
}
